using System.Collections.ObjectModel;
using System.Windows;
using WpfApp1.Models;
using WpfApp1.Services;

namespace WpfApp1.ViewModels
{
    public class TeacherDashboardViewModel
    {
        private readonly ITeacherCourseService _teacherCourseService;
        private readonly ICourseSelectService _courseSelectService;
        private string _currentTeacherNumber;
        private string _currentCourseNumber;  // 添加字段保存当前课程号

        public ObservableCollection<Course> TeacherCourses { get; } = new ObservableCollection<Course>();
        public ObservableCollection<StudentCourseSelection> StudentGrades { get; } = new ObservableCollection<StudentCourseSelection>();

        public TeacherDashboardViewModel(
            string teacherNumber,
            ITeacherCourseService teacherCourseService,
            ICourseSelectService courseSelectService)
        {
            _currentTeacherNumber = teacherNumber;
            _teacherCourseService = teacherCourseService;
            _courseSelectService = courseSelectService;
        }

        // 加载教师的课程
        public void LoadTeacherCourses()
        {
            TeacherCourses.Clear();
            var courses = _teacherCourseService.GetTeacherCourseByNumber(_currentTeacherNumber);
            foreach (var course in courses)
            {
                TeacherCourses.Add(course);
            }
        }

        // 加载所有任课学生
        public void LoadAllTeacherStudents()
        {
            StudentGrades.Clear();
            foreach (var course in TeacherCourses)
            {
                var courseSelection = new CourseSelection(string.Empty, course.CourseNumber, 0);
                var students = _courseSelectService.GetStudentCoursesByCourseNumber(courseSelection);
                foreach (var student in students)
                {
                    StudentGrades.Add(student);
                }
            }
        }


        // 查看选择某门课程的学生信息
        public void LoadCourseStudents(string courseNumber)
        {
            _currentCourseNumber = courseNumber;
            StudentGrades.Clear();
            var courseSelection = new CourseSelection(string.Empty, courseNumber, 0);
            var students = _courseSelectService.GetStudentCoursesByCourseNumber(courseSelection);
            foreach (var student in students)
            {
                StudentGrades.Add(student);
            }
        }

        // 给学生评分
        public void GradeStudent(string studentNumber, string courseNumber, int grade)
        {
            var courseSelection = new CourseSelection(studentNumber, courseNumber, grade);
            _teacherCourseService.UpdateStudentCourse(courseSelection);
            //根据当前状态刷新显示
            if (string.IsNullOrEmpty(_currentCourseNumber))
            {
                LoadAllTeacherStudents();
            }
            else
            {
                LoadCourseStudents(_currentCourseNumber);
            }
        }

        // 添加按学号搜索方法
        public void SearchStudentByNumber(string studentNumber)
        {
            StudentGrades.Clear();
            if (string.IsNullOrEmpty(_currentCourseNumber))
            {
                // 如果没有选择具体课程，则在所有任课学生中搜索
                foreach (var course in TeacherCourses)
                {
                    var courseSelection = new CourseSelection(string.Empty, course.CourseNumber, 0);
                    var students = _courseSelectService.GetStudentCoursesByCourseNumber(courseSelection);
                    var filteredStudents = students.Where(s => s.StudentNumber.Contains(studentNumber));
                    foreach (var student in filteredStudents)
                    {
                        StudentGrades.Add(student);
                    }
                }
            }
            else
            {
                // 如果选择了具体课程，则在该课程学生中搜索
                var courseSelection = new CourseSelection(string.Empty, _currentCourseNumber, 0);
                var students = _courseSelectService.GetStudentCoursesByCourseNumber(courseSelection);
                var filteredStudents = students.Where(s => s.StudentNumber.Contains(studentNumber));
                foreach (var student in filteredStudents)
                {
                    StudentGrades.Add(student);
                }
            }
        }

        // 显示全部学生
        public void ShowAllStudents()
        {
            _currentCourseNumber = string.Empty; // 清空当前选中的课程
            LoadAllTeacherStudents(); // 显示所有任课学生
        }

    }
}